package com.learn.project2.service.admin.aftersale;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketAftersale;
import com.learn.project2.bean.MarketAftersaleExample;
import com.learn.project2.mapper.MarketAftersaleMapper;
import com.learn.project2.model.bo.admin.aftersale.AftersaleListBO;
import com.learn.project2.model.vo.admin.aftersale.AftersaleListVO;
import com.learn.project2.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * \* Author: Valley
 * \* Date: 2022/1/7
 * \* Time: 17:49
 * \* Description:
 */
@Service
public class AftersaleServiceImpl implements AftersaleService {

    @Autowired
    MarketAftersaleMapper aftersaleMapper;

    /**
     * @description: 售后管理-列表
     * @param: [listBO]
     * @return: com.learn.project2.model.vo.admin.aftersale.AftersaleListVO
     * @author 王力
     * @date: 2022-01-07 18:02:19
     */
    @Override
    public AftersaleListVO aftersaleList(AftersaleListBO listBO) {
        PageHelper.startPage(listBO.getPage(),listBO.getLimit());
        MarketAftersaleExample aftersaleExample = new MarketAftersaleExample();
        MarketAftersaleExample.Criteria criteria = aftersaleExample.createCriteria().andDeletedEqualTo(false);
        if (listBO.getAftersaleSn() != null) criteria.andAftersaleSnEqualTo(listBO.getAftersaleSn());
        if (listBO.getOrderId() != null) criteria.andOrderIdEqualTo(listBO.getOrderId());
        List<MarketAftersale> marketAftersales = aftersaleMapper.selectByExample(aftersaleExample);
        PageInfo<MarketAftersale> pageInfo = new PageInfo<>(marketAftersales);
        AftersaleListVO aftersaleListVO = new AftersaleListVO();
        aftersaleListVO.setList(marketAftersales);
        aftersaleListVO.setLimit(listBO.getLimit());
        aftersaleListVO.setPage(listBO.getPage());
        aftersaleListVO.setPages(pageInfo.getPages());
        aftersaleListVO.setTotal(pageInfo.getTotal());
        return aftersaleListVO;
    }

    /**
     * @description: 售后管理-批量通过
     * @param: [ids]
     * @return: java.lang.Integer
     * @author 王力
     * @date: 2022-01-07 18:42:27
     */
    @Override
    public Integer batchRecept(List<Integer> ids) {
        for (Integer id : ids) {
            MarketAftersale marketAftersale = aftersaleMapper.selectByPrimaryKey(id);
            marketAftersale.setStatus((short)2);
            marketAftersale.setHandleTime(StringUtils.dateToDate(new Date()));
            marketAftersale.setUpdateTime(StringUtils.dateToDate(new Date()));
            aftersaleMapper.updateByPrimaryKey(marketAftersale);
        }
        return null;
    }

    @Override
    public Integer batchReject(List<Integer> ids) {
        for (Integer id : ids) {
            MarketAftersale marketAftersale = aftersaleMapper.selectByPrimaryKey(id);
            marketAftersale.setStatus((short)4);
            marketAftersale.setHandleTime(StringUtils.dateToDate(new Date()));
            marketAftersale.setUpdateTime(StringUtils.dateToDate(new Date()));
            aftersaleMapper.updateByPrimaryKey(marketAftersale);
        }
        return null;
    }

    @Override
    public Integer aftersaleRefund(MarketAftersale aftersale) {
        MarketAftersale aftersale1 = aftersaleMapper.selectByPrimaryKey(aftersale.getId());
        aftersale1.setStatus((short)3);
        aftersale1.setHandleTime(StringUtils.dateToDate(new Date()));
        aftersale1.setUpdateTime(StringUtils.dateToDate(new Date()));
        int i = aftersaleMapper.updateByPrimaryKey(aftersale1);
        return i;
    }

    /**
     * @description: 审核通过
     * @param: [aftersale]
     * @return: java.lang.Integer
     * @author 王力
     * @date: 2022-01-12 16:46:51
     */
    @Override
    public Integer aftersaleRecept(MarketAftersale aftersale) {
        MarketAftersale marketAftersale = aftersaleMapper.selectByPrimaryKey(aftersale.getId());
        marketAftersale.setStatus((short)2);
        marketAftersale.setHandleTime(StringUtils.dateToDate(new Date()));
        marketAftersale.setUpdateTime(StringUtils.dateToDate(new Date()));
        int i = aftersaleMapper.updateByPrimaryKey(marketAftersale);
        return i;
    }

    /**
     * @description: 审核拒绝
     * @param: [aftersale]
     * @return: java.lang.Integer
     * @author 王力
     * @date: 2022-01-12 16:49:03
     */
    @Override
    public Integer aftersaleReject(MarketAftersale aftersale) {
        MarketAftersale marketAftersale = aftersaleMapper.selectByPrimaryKey(aftersale.getId());
        marketAftersale.setStatus((short)4);
        marketAftersale.setHandleTime(StringUtils.dateToDate(new Date()));
        marketAftersale.setUpdateTime(StringUtils.dateToDate(new Date()));
        int i = aftersaleMapper.updateByPrimaryKey(marketAftersale);
        return i;
    }


}
